package com.zusmart.core.config.support;

import com.zusmart.base.util.StringUtils;
import com.zusmart.core.config.ConfigName;

public abstract class AbstractConfigName implements ConfigName {

	protected String originConfigName;
	protected String actualConfigName;
	protected String actualConfigPath;
	protected String actualConfigFile;
	protected String actualConfigValue;
	protected String actualPrefixValue;

	protected AbstractConfigName(String originConfigName) {
		if (StringUtils.isBlank(originConfigName)) {
			throw new IllegalArgumentException("origin config name must not be blank");
		}
		this.originConfigName = originConfigName;
		this.resolve();
	}

	@Override
	public String getOriginConfigName() {
		return this.originConfigName;
	}

	@Override
	public String getActualConfigName() {
		return this.actualConfigName;
	}

	@Override
	public String getActualConfigPath() {
		return this.actualConfigPath;
	}

	@Override
	public String getActualConfigFile() {
		return this.actualConfigFile;
	}

	@Override
	public String getActualConfigValue() {
		return this.actualConfigValue;
	}

	@Override
	public String getActualPrefixValue() {
		return this.actualPrefixValue;
	}

	@Override
	public boolean hasActualConfigValue() {
		return StringUtils.isNotBlank(this.actualConfigValue);
	}

	@Override
	public boolean hasActualConfigFile() {
		return StringUtils.isNotBlank(this.actualConfigFile);
	}

	@Override
	public boolean hasActualPrefixValue() {
		return StringUtils.isNotBlank(this.actualPrefixValue);
	}

	@Override
	public int hashCode() {
		return this.toString().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (obj instanceof AbstractConfigName) {
			return this.toString().equals(obj.toString());
		}
		return false;
	}

	@Override
	public String toString() {
		StringBuffer stringBuffer = new StringBuffer();
		if (this.hasActualConfigFile()) {
			stringBuffer.append(this.actualConfigFile).append("[");
		}
		stringBuffer.append(this.actualConfigName);
		if (this.hasActualConfigValue()) {
			stringBuffer.append(":").append(this.actualConfigValue);
		}
		if (this.hasActualConfigFile()) {
			stringBuffer.append("]");
		}
		if (this.hasActualPrefixValue()) {
			stringBuffer.append("(").append(this.actualPrefixValue).append(")");
		}
		return stringBuffer.toString();
	}

	protected abstract void resolve();

}